package com.demo.test.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/**
 * @author gy
 * @date 2025/2/16
 */
@Slf4j
public class Other2 {
    @Test
    public void m1(){
        long startTime = System.currentTimeMillis();
        String method = "PUT";
        switch (method) {
            case "GET":
                // GET 请求: 从 URL 查询参数中提取
                break;
            case "POST":
            case "PUT":
            case "PATCH":
            case "DELETE":
                // POST/PUT 等请求: 从请求体中提取
                System.out.println("aaaaaaa");
                break;
            default:
                System.out.println("没有命中");
                return;
        }

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m2(){
        long startTime = System.currentTimeMillis();

        String timestampStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(System.currentTimeMillis());
        System.out.println(timestampStr);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m3(){
        long startTime = System.currentTimeMillis();

        long timestamp = System.currentTimeMillis();
        String timestampStr = String.valueOf(timestamp);
        System.out.println("Current Timestamp: " + timestampStr);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m4(){
        long startTime = System.currentTimeMillis();

        ArrayList<String> strings = new ArrayList<>();
        strings.add("com.xueersi.ubiqask.sdk.sample");
        System.out.println(JSON.toJSONString(strings));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


    @Test
    public void m5(){
        long startTime = System.currentTimeMillis();


//        log.error("RuntimeExceptionHandler -> e={}={}", e.getLocalizedMessage(), (request != null ? request.getRequestURL() : "no-url"), e);
//        doOriginalException(e);
//        if ("dev".equals(env)) {
//            e.printStackTrace();
//        }


        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    public static void main(String args[]) throws Exception {
        try {
            subNonNegativeNumber(-3, 2);
        } catch (Exception ex) {
            log.error("RuntimeExceptionHandler -> e={}={}", ex.getLocalizedMessage(), "1111", ex);

            System.out.println("localized message :" + ex.getLocalizedMessage());
        }
    }

    // This method substract two non-negative numbers
    public static void subNonNegativeNumber(int s1, int s2) throws Exception {
        // if anyone of the given number is
        // negative so it throw an exception
        if (s1 < 0 || s2 < 0) {
            throw new Exception("No's are less than 0");
        }
        // if both are non-negative , it substracts
        else {
            int res = s1 - s2;
            System.out.println("substract :" + res);
        }
    }


    @Test
    public void m10(){
        long startTime = System.currentTimeMillis();

        String a = "IlIlIlIlIIIlll";
        for (char c : a.toCharArray()) {
            if (c == 'I'){
                System.out.println("11111111");
            }else {
                System.out.println("22222222");
            }
        }
        System.out.println(BigDecimal.ZERO);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m6(){
        long startTime = System.currentTimeMillis();

//        String nonce = UUID.randomUUID().toString().replace("-", "");
        String nonce = UUID.randomUUID().toString();
        System.out.println(nonce);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m7(){
        long startTime = System.currentTimeMillis();

        Random random = new Random();

        // 使用 Random 的 nextBoolean() 方法生成 true 或 false
        boolean randomBoolean = random.nextBoolean();

        System.out.println("随机生成的布尔值: " + randomBoolean);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m8(){
        long startTime = System.currentTimeMillis();

        String data = "session_id:test_session_002\n"
                + "interaction_id:test_interaction_002\n"
                + "segment_id:test_message_002\n"
                + "event:off_screen_student_round_intent_classify\n"
                + "data:{\"combined_ocr_asr\": \"我想想 我写完了 你是什么小狗狗 fuck you\", "
                + "\"intent_result\": \"非正面回答问题\", \"weather_compensate\": \"轻补偿\"}";

        JSONObject result = new JSONObject(true); // 保持插入顺序

        String[] lines = data.split("\n");
        for (String line : lines) {
            String[] parts = line.split(":", 2);
            if (parts.length == 2) {
                String key = parts[0].trim();
                String value = parts[1].trim();

                if ("data".equals(key)) {
                    try {
                        JSONObject nested = JSON.parseObject(value);
                        result.put(key, nested);
                    } catch (Exception e) {
                        result.put(key, value);
                    }
                } else {
                    result.put(key, value);
                }
            }
        }

        System.out.println(JSON.toJSONString(result, true));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


    @Test
    public void m0728(){
        long startTime = System.currentTimeMillis();

        String a = "[{\"question\": \"1\", \"solution\": [{\"step\": \"1\", \"title\": \"理解题意\", \"detail\": \"已知函数 $f(x)$ 的定义如下：\\n$$\\nf(x) = \\n\\\\begin{cases} \\nx^3, & x \\\\geq 0 \\\\\\\\\\n-x, & x < 0 \\n\\\\end{cases}\\n$$\\n函数 $g(x) = f(x) - |kx^2 - 2x| (k \\\\in \\\\mathbb{R})$ 恰有 4 个零点，求 $k$ 的取值范围。\"}, {\"step\": \"2\", \"title\": \"分析 $g(x)$ 的零点\", \"detail\": \"函数 $g(x)$ 的零点即为 $f(x) = |kx^2 - 2x|$ 的解。我们需要分别讨论 $x \\\\geq 0$ 和 $x < 0$ 的情况。\"}, {\"step\": \"3\", \"title\": \"讨论 $x \\\\geq 0$ 的情况\", \"detail\": \"当 $x \\\\geq 0$ 时，$f(x) = x^3$，所以 $g(x) = x^3 - |kx^2 - 2x|$。我们需要 $x^3 = |kx^2 - 2x|$。\"}, {\"step\": \"4\", \"title\": \"讨论 $x < 0$ 的情况\", \"detail\": \"当 $x < 0$ 时，$f(x) = -x$，所以 $g(x) = -x - |kx^2 - 2x|$。我们需要 $-x = |kx^2 - 2x|$。\"}, {\"step\": \"5\", \"title\": \"分析 $|kx^2\", \"detail\": \"$2x|$ 的取值范围-由于 $|kx^2 - 2x|$ 是一个绝对值函数，我们需要分别讨论 $kx^2 - 2x \\\\geq 0$ 和 $kx^2 - 2x < 0$ 的情况。\"}, {\"step\": \"6\", \"title\": \"讨论 $kx^2\", \"detail\": \"$2x \\\\geq 0$ 的情况-当 $kx^2 - 2x \\\\geq 0$ 时，$|kx^2 - 2x| = kx^2 - 2x$。我们需要 $x^3 = kx^2 - 2x$ 或 $-x = kx^2 - 2x$。\"}, {\"step\": \"7\", \"title\": \"讨论 $kx^2\", \"detail\": \"$2x < 0$ 的情况-当 $kx^2 - 2x < 0$ 时，$|kx^2 - 2x| = -(kx^2 - 2x)$。我们需要 $x^3 = -(kx^2 - 2x)$ 或 $-x = -(kx^2 - 2x)$。\"}, {\"step\": \"8\", \"title\": \"求解方程\", \"detail\": \"我们需要分别求解以下方程：\\n1. $x^3 = kx^2 - 2x$\\n2. $-x = kx^2 - 2x$\\n3. $x^3 = -(kx^2 - 2x)$\\n4. $-x = -(kx^2 - 2x)$\"}, {\"step\": \"9\", \"title\": \"分析方程的解\", \"detail\": \"通过分析这些方程的解，我们可以得出 $k$ 的取值范围。具体来说，我们需要确保这些方程在 $x \\\\geq 0$ 和 $x < 0$ 的情况下各有 2 个解。\"}, {\"step\": \"10\", \"title\": \"总结 $k$ 的取值范围\", \"detail\": \"通过分析和计算，我们可以得出 $k$ 的取值范围为：\\n\\n$k \\\\in (-\\\\infty, 0) \\\\cup (2\\\\sqrt{2}, +\\\\infty)$\"}, {\"step\": \"11\", \"title\": \"最终答案\", \"detail\": \"综上所述，$k$ 的取值范围是：\\n\\n$(-\\\\infty, 0) \\\\cup (2\\\\sqrt{2}, +\\\\infty)$\"}]}]";


        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


}
